package com.datang.pet.api.chatController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-27 13:48
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.pojo.Chat;
import com.datang.pet.service.ChatService;
import com.datang.pet.vo.getUserChatListRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/addChat")
    public Result addChat(@RequestBody Chat chat) throws Exception {
        try {
            chat.setSenderId(MD5.decryptData(chat.getSenderId()));
        } catch (Exception e) {
            throw new Exception("用户Id解密出错！");
        }
        chatService.addChat(chat);
        return Result.ok("ok");
    }

    @PostMapping("/getUserChatList")
    public Result getUserChatList(@RequestBody getUserChatListRequest getUserChatListRequest) throws Exception {
        try {
            getUserChatListRequest.setUserId(MD5.decryptData(getUserChatListRequest.getUserId()));
        } catch (Exception e) {
            throw new Exception("用户Id解密出错！");
        }
        return Result.ok(chatService.getUserChatList(getUserChatListRequest.getUserId(),getUserChatListRequest.getTime()));
    }

    @GetMapping("/getChatList")
    public Result getChatList(Chat chat) throws Exception {
        return Result.ok(chatService.getChatList(chat));
    }
}
